package com.zy.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.zy.constant.MessageConstant;
import com.zy.constant.RedisMessageConstant;
import com.zy.entity.Result;
import com.zy.pojo.Order;
import com.zy.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

	@Reference
	private OrderService orderService;
	@Autowired
	private JedisPool jedisPool;

	@RequestMapping("/submit")
	public Result submitOrder(@RequestBody Map map){
		//拿到输入验证码
		String code = (String) map.get("validateCode");
		//从redis中拿到的验证码
		String telephone = (String) map.get("telephone");
		String yzm = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);

		//判断两个是否相等
		if(code!=null && yzm!=null && code.equals(yzm) ){
			//验证码正确
			try {
				//当前预约的类型为微信预约
				map.put("orderType", Order.ORDERTYPE_WEIXIN);
				return orderService.order(map);
			} catch (Exception e) {
				e.printStackTrace();
				return new Result(false,"预约失败");
			}
		}else{
			return new Result(false, MessageConstant.VALIDATECODE_ERROR);
		}
	}

	@RequestMapping("/findById")
	public Result findById(Integer id){
		try {
			Map map = orderService.findById(id);
			return new Result(false, MessageConstant.QUERY_ORDER_SUCCESS,map);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
		}
	}

}
